package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.RankingBean;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.view.MyAutofitTextView.AutofitTextView;

import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.ActivityHolder> {
    private Context context;
    private List<RankingBean.ListBean> mData;


    public RankingAdapter(List<RankingBean.ListBean> data) {
        this.mData = data;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ranking, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {

            RankingBean.ListBean listBean = mData.get(position);
            String name = listBean.getName().toString().trim();
            String mileage = listBean.getTotalMileage();
            String duration = listBean.getTotalDuration();
            String grade = listBean.getGrade();


            String nickName ="";
            if (name.length() >= 1) {
                nickName = name.substring(0,1);
            }

            if(!StringUtils.isEmpty(nickName)){
                holder.tvNickName.setText(nickName);
            }

            if(!StringUtils.isEmpty(name)){
                holder.tvName.setText(name);
            }

            if(!StringUtils.isEmpty(mileage)){
                holder.tvMileage.setText(mileage+"km");
            }

            if(!StringUtils.isEmpty(duration)){
                holder.tvDuration.setText(MyUtills.formatSeconds5(Long.parseLong(duration)));
            }

            if(!StringUtils.isEmpty(grade)){
                holder.tvGrade.setText(grade);
            }

            holder.tvNum.setText((position + 4) + "");

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<RankingBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View view;
        private final AutofitTextView tvNum;
        private final AutofitTextView tvNickName;
        private final TextView tvName;
        private final TextView tvMileage;
        private final TextView tvDuration;
        private final TextView tvGrade;

        public ActivityHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvNum = (AutofitTextView) view.findViewById(R.id.tv_num);
            tvNickName = (AutofitTextView) view.findViewById(R.id.tv_nick_name);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvMileage = (TextView) view.findViewById(R.id.tv_mileage);
            tvDuration = (TextView) view.findViewById(R.id.tv_duration);
            tvGrade = (TextView) view.findViewById(R.id.tv_grade);
        }
    }

}
